<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="订单号">
              <a-input style="width:100%" placeholder="请输入订单号" v-model="queryParam.orderId"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="订单状态">
              <a-select v-model="queryParam.status" placeholder="全部状态">
                <a-select-option :value="item.val" v-for="(item, index) in statusUl" :key="index">{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="患者名称">
              <a-input style="width:100%" placeholder="请输入患者名称" v-model="queryParam.personName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="患者手机">
              <a-input style="width:100%" placeholder="请输入患者手机" v-model="queryParam.personMobile"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="开单人手机号">
              <a-input style="width:100%" placeholder="请输入开单人手机号" v-model="queryParam.mobile"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="配送码">
              <a-input style="width:100%" placeholder="请输入配送码" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="创建时间">
              <a-range-picker @change="panelChange" v-model="queryParamRangePicker" />
            </a-form-item>
          </a-col>
          
          <!-- <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="医生/护士名称">
              <a-input style="width:100%" placeholder="请输入医生/护士名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <a-form-item label="医院名称">
              <a-input style="width:100%" placeholder="请输入医院名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
           -->
          <a-col :xl="6" :lg="6" :md="12" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" @click="orderExport" icon="search">导出订单</a-button>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" bordered rowKey="id" :scroll="{ x: 1300 }" :columns="columns" :dataSource="tableData" :pagination="ipagination" :loading="loading" @change="handleTableChange">
        <!--操作-->
        <span slot="action" slot-scope="text, record">
          <a-button type="primary" @click="handDetail(record)">详情</a-button>
          <a-button type="primary" @click="handCance(record)" v-show="record.status == 0">取消订单</a-button>
          <a-button type="primary" @click="handSend(record)" v-show="record.status == 2">配送</a-button>
          <a-popconfirm title="确认退款?" ok-text="确定" cancel-text="取消" @confirm="handRefund(record)">
            <a-button type="danger" v-show="record.status == 2 || record.status == 3">退款</a-button>
          </a-popconfirm>
        </span>
        <!--图片预览块-->
        <span slot="imgPath" slot-scope="text, record">
          <img :src="record.img" width="80px" height="80px" />
        </span>
        <!--价格-->
        <span slot="money" slot-scope="text">
          <span>{{text | filtersMoney}}</span>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <orderListModal ref="updateForm" @closeModal="closeModal" />
    <orderRefundModal ref="refundmodalForm" @closeModal="closeModal" />
  </a-card>
</template>

<script>
import orderRefundModal from './modules/orderRefundModal';
import orderListModal from './modules/orderListModal';
import ApiTree from '@/api/order/orderList';
import { Slime, crud } from '@/mixins/slime';
import { evaFiltersMixin } from '@/mixins/evaMixin';
import moment from 'moment'

export default {
  name: 'orderList',
  mixins: [Slime, evaFiltersMixin],
  components: {
    orderListModal,
    orderRefundModal,
  },
  data() {
    return {
      statusUl: [
        { name: '待付款', val: 0 },
        { name: '已取消', val: 1 },
        { name: '待配送', val: 2 },
        { name: '出库失败,待退款', val: 3 },
        { name: '已退款', val: 4 },
        { name: '已配送', val: 5 },
      ],
      tableData: [],
      columns: [
        {
          title: '订单号',
          align: 'center',
          dataIndex: 'id',
        },
        {
          title: '金额',
          align: 'center',
          dataIndex: 'money',
          scopedSlots: { customRender: 'money' },
        },
        {
          title: '姓名',
          align: 'center',
          dataIndex: 'personName',
        },
        {
          title: '手机号',
          align: 'center',
          dataIndex: 'personMobile',
        },
        {
          title: '支付方式',
          align: 'center',
          dataIndex: 'payChannel',
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          customRender: function (status) {
            switch (status) {
              case 0:
                return '待付款';
              case 1:
                return '已取消';
              case 2:
                return '待配送';
              case 3:
                return '出库失败,待退款';
              case 4:
                return '已退款';
              case 5:
                return '已配送';
              default:
                return '';
            }
          },
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    // 加载表格数据
    loadData(arg) {
      crud(ApiTree);
      this.loadDataPack(arg).then((res) => {
        let tableData = [];
        if (res.result) {
          tableData = res.result.records;
        }
        this.tableData = tableData;
      });
    },
    //导出订单
    orderExport() {
      let param = this.queryParam;
      ApiTree.getDetailExport(param).then((res) => {
        console.log(res)
        const blob = new Blob([res], {
          type: "application/vnd.ms-excel;charset=utf-8",
        });
        console.log(blob)
        //对于<a>标签，只有 Firefox 和 Chrome（内核） 支持 download 属性
        //IE10以上支持blob但是依然不支持download
        if ("download" in document.createElement("a")) {
          //支持a标签download的浏览器
          const link = document.createElement("a"); //创建a标签
          link.download = "分销订单导出.xls"; //a标签添加属性
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          document.body.appendChild(link);
          link.click(); //执行下载
          URL.revokeObjectURL(link.href); //释放url
          document.body.removeChild(link); //释放标签
        } else {
          navigator.msSaveBlob(blob, fileName);
        }
      });
    },
    //选择日期
    panelChange(date) {
      console.log(date)
      let startDate = moment(date[0]).format('YYYY-MM-DD');
      let endDate = moment(date[1]).format('YYYY-MM-DD');
      console.log(startDate)
      this.queryParam = {
        ...this.queryParam,
        startTime: startDate,
        endTime: endDate,
      };
    },
    //详情
    handDetail(record) {
      this.$refs.updateForm.crudUpdate(record);
    },
    //发货
    handSend(record) {
      ApiTree.getDetailSend(record.id).then((res) => {
        this.$message.success('发货成功');
      });
    },
    //取消订单
    handCance(record) {
      let param = {
        orderId: record.id,
        reason: '',
      };
      ApiTree.getDetailCancel(param).then((res) => {
        this.$message.success('取消成功');
      });
    },
    //打印单据信息
    handPrint(record) {
      ApiTree.getMostCodes(id).then((res) => {});
    },
    //退款
    handRefund(record) {
      this.$refs.refundmodalForm.open(record);
    },
  },
};
</script>
<style scoped>
</style>